#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import ffmpeg
import sys

parser = argparse.ArgumentParser(
    description='Read individual video frame into memory as jpeg and write to stdout')
parser.add_argument('in_filename', help='Input filename')
parser.add_argument('frame_num', help='Frame number')
parser.add_argument('dir', help='output dir')


def read_frame_as_jpeg(in_filename, frame_num,dir):
    out, err = (
        ffmpeg
        .input(in_filename)
        .filter('scale', size='128x64')
        .output(dir+'/%03d.jpeg', vframes=frame_num, format='image2', vcodec='mjpeg')
        .run(capture_stdout=True)
    )
    return out


if __name__ == '__main__':
    args = parser.parse_args()
    print("视频文件%s ,输出帧数%s ,输出目录 %s"%(args.in_filename, args.frame_num,args.dir))
    out = read_frame_as_jpeg(args.in_filename, args.frame_num,args.dir)
    sys.stdout.buffer.write(out)
    print("视频文件%s ,输出帧数%s ,输出目录 %s"%(args.in_filename, args.frame_num,args.dir))